package com.example.piginzoo.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.transition.CircularPropagation;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.tencent.gcloud.voice.GCloudVoiceEngine;
import com.tencent.gcloud.voice.GCloudVoiceErrno;


import java.util.Random;

public class MainActivity extends Activity implements View.OnClickListener{

    private GCloudVoiceEngine engine;
    private String appId = "1513949336";//测试用appid
    private String appKey = "42775eb7b595a43323964843e48de9da";//测试用appkey
    private String openId = "";
    private Button enteroomButton;
    private Button quitroomButton;
    private Button openmicButton;
    private Button closemicButton;
    private Button closespeakerButton;
    private Button openspeakerButton;
    private static final String TAG ="MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GCloudVoiceEngine.getInstance().init(getApplicationContext(),this);
        engine = GCloudVoiceEngine.getInstance();
        openId  = ""+ new Random().nextLong();//模拟随机产生一个用户id
        engine.SetAppInfo(appId,appKey,openId);

        int r = engine.Init();
        Log.i(TAG, "init() return: "+r);
        r = engine.SetNotify(new Notify());
        Log.i(TAG, "setNotify() return: "+r);

        r = engine.SetMode(0);// realtime mode for TeamRoom or NationalRoom
        Log.i(TAG, "setMode() return: "+r);

        enteroomButton = (Button) findViewById(R.id.button_enteroom);
        quitroomButton = (Button) findViewById(R.id.button_quitroom);
        openmicButton  = (Button) findViewById(R.id.button_openmic);
        closemicButton = (Button) findViewById(R.id.button_closemic);
        openspeakerButton = (Button) findViewById(R.id.button_openspeaker);
        closespeakerButton = (Button) findViewById(R.id.button_closespeaker);
        enteroomButton.setOnClickListener(this);
        quitroomButton.setOnClickListener(this);
        openmicButton.setOnClickListener(this);
        closemicButton.setOnClickListener(this);
        openspeakerButton.setOnClickListener(this);
        closespeakerButton.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        int code;
        switch (v.getId()){
            case R.id.button_enteroom:
                code = engine.JoinTeamRoom("testroom",10000);
                if (code == GCloudVoiceErrno.GCLOUD_VOICE_SUCC){
                    Log.i(TAG,"click join room success");
                    showMsg("join room success");
                }else {
                    Log.i(TAG,"click join room fail"+code);
                    showMsg("join room fail"+code);
                }
                break;
            case R.id.button_quitroom:
                code =engine.QuitRoom("testroom",10000);
                if (code==GCloudVoiceErrno.GCLOUD_VOICE_SUCC){
                    Log.i(TAG,"click quit room success");
                    showMsg("quit room success");
                }else {
                    Log.i(TAG,"click quit room fail "+code);
                    showMsg("click quit room fail "+code);
                }
                break;
            case R.id.button_openmic:
                code = engine.OpenMic();
                if (code==GCloudVoiceErrno.GCLOUD_VOICE_SUCC){
                    Log.i(TAG,"click open mic success");
                    showMsg("click open mic success");
                }
                else{
                    Log.i(TAG,"click open mic fail"+code);
                    showMsg("click open mic fail"+code);
                }
                break;
            case R.id.button_closemic:
                code = engine.CloseMic();
                if (code==GCloudVoiceErrno.GCLOUD_VOICE_SUCC){
                    Log.i(TAG,"click close mic success");
                    showMsg("click close mic success");
                } else{
                    Log.i(TAG,"click close mic fail"+code);
                    showMsg("click close mic fail"+code);
                }
                break;
            case R.id.button_openspeaker:
                code = engine.OpenSpeaker();
                if (code==GCloudVoiceErrno.GCLOUD_VOICE_SUCC){
                    Log.i(TAG,"click OpenSpeaker success");
                    showMsg("click OpenSpeaker success");
                } else{
                    Log.i(TAG,"click OpenSpeaker fail"+code);
                    showMsg("click OpenSpeaker fail"+code);
                }
                break;
            case R.id.button_closespeaker:
                code = engine.CloseSpeaker();
                if (code==GCloudVoiceErrno.GCLOUD_VOICE_SUCC){
                    Log.i(TAG,"click CloseSpeaker success");
                    showMsg("click CloseSpeaker success");
                } else{
                    Log.i(TAG,"click CloseSpeaker fail"+code);
                    showMsg("click CloseSpeaker fail"+code);
                }
                break;
        }
    }

    private void showMsg(String msg){
        Toast.makeText(MainActivity.this,msg, Toast.LENGTH_SHORT).show();
    }
}
